<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Car_info extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Car_info_model');
	}
	//查询列表
	public function index()
	{
		
		$this->load->model('Car_brand_model');
		$this->load->model('Car_series_model');

		$page_size = $this->page_size;

		$params = array( 'length'=>$page_size);
		$params['carname'] = $this->input->get_post('carname');
		$params['brandid'] = $this->input->get_post('brandid');
		$params['seriesid'] = $this->input->get_post('seriesid');
		$params['start'] =  intval($this->input->get_post('per_page'));

		$result = $this->Car_info_model->select_page($params);
		
		$result['params'] = $params;
		//分页设置
		$base_url =  site_url("c=car_info&m=index&carname=$params[carname]&brandid=$params[brandid]&seriesid=$params[seriesid]");
		$result['pagelink'] = $this->create_page_links($base_url,$result['count']);

		$result['brandlist'] =$this->Car_brand_model->select_list();
		$result['serieslist'] =$this->Car_series_model->select_list_group_by_series();
		
		$this->show_view('carInfoList',$result);
		
	}

	//编辑
	public function edit()
	{

		$this->form_validation->set_rules('carname', '车辆名称', 'required');

		$carid = $this->input->get_post('carid');

		$carinfoeditsubmit = $this->input->post('carinfoeditsubmit');
		$resultinfo['carinfo'] = $this->Car_info_model->select_by_id($carid);

		$resultinfo['method'] ='edit';
		if($carinfoeditsubmit  && $this->form_validation->run())
		{
			$carinfo = array();
			$carinfo['carname'] = $this->input->get_post('carname');
			$carinfo['brandid'] = $this->input->get_post('brandid');
			$carinfo['seriesid'] = $this->input->get_post('seriesid');
			$carinfo['typeid'] = $this->input->get_post('typeid');
			$carinfo['structureid'] = $this->input->get_post('structureid');
			$carinfo['carengine'] = $this->input->get_post('carengine');
			$carinfo['transmission'] = $this->input->get_post('transmission');
			$carinfo['maxspeed'] = $this->input->get_post('maxspeed');
			$carinfo['speedup'] = $this->input->get_post('speedup');
			$carinfo['fueluse'] = $this->input->get_post('fueluse');
			$carinfo['realspeedup'] = $this->input->get_post('realspeedup');
			$carinfo['realspeeddown'] = $this->input->get_post('realspeeddown');
			$carinfo['realfueluse'] = $this->input->get_post('realfueluse');
			$carinfo['wholewarranty'] = $this->input->get_post('wholewarranty');
			$carinfo_extend = $this->input->get_post('carinfo_extend');
			$result = $this->Car_info_model->update($carid,$carinfo,$carinfo_extend);
			$this->show_message("编辑车辆系列信息成功",site_url("c=car_info&m=index"));
			
		}
		else
		{	
			$this->load->model('Car_brand_model');
			$this->load->model('Car_series_model');
			$this->load->model('Car_type_model');
			$this->load->model('Car_structure_model');
			$resultinfo['brandlist'] =$this->Car_brand_model->select_list();
			$resultinfo['serieslist'] =$this->Car_series_model->select_list_group_by_series();
			$resultinfo['typelist'] =$this->Car_type_model->select_list();
			$resultinfo['structurelist'] =$this->Car_structure_model->select_list();
			
			$this->show_view('carInfoEdit',$resultinfo);
			
		}
	}
	//新增车辆信息
	public function add()
	{
		$this->form_validation->set_rules('carname', '车辆名称', 'required');
		
		$carinfoeditsubmit = $this->input->post('carinfoeditsubmit');
		$resultinfo  =array('method'=>'add');
		if($carinfoeditsubmit  && $this->form_validation->run())
		{
			$carinfo = array();
			$carinfo['carname'] = $this->input->get_post('carname');
			$carinfo['brandid'] = $this->input->get_post('brandid');
			$carinfo['seriesid'] = $this->input->get_post('seriesid');
			$carinfo['typeid'] = $this->input->get_post('typeid');
			$carinfo['structureid'] = $this->input->get_post('structureid');
			$carinfo['carengine'] = $this->input->get_post('carengine');
			$carinfo['transmission'] = $this->input->get_post('transmission');
			$carinfo['maxspeed'] = $this->input->get_post('maxspeed');
			$carinfo['speedup'] = $this->input->get_post('speedup');
			$carinfo['fueluse'] = $this->input->get_post('fueluse');
			$carinfo['realspeedup'] = $this->input->get_post('realspeedup');
			$carinfo['realspeeddown'] = $this->input->get_post('realspeeddown');
			$carinfo['realfueluse'] = $this->input->get_post('realfueluse');
			$carinfo['wholewarranty'] = $this->input->get_post('wholewarranty');
			$carinfo_extend = $this->input->get_post('carinfo_extend');
			$result = $this->Car_info_model->insert($carinfo,$carinfo_extend);

			$this->show_message("新增车辆系列信息成功",site_url("c=car_info&m=index"));
			
		}
		else
		{		
			$this->load->model('Car_brand_model');
			$this->load->model('Car_series_model');
			$this->load->model('Car_type_model');
			$this->load->model('Car_structure_model');
			$resultinfo['brandlist'] =$this->Car_brand_model->select_list();
			$resultinfo['serieslist'] =$this->Car_series_model->select_list_group_by_series();
			$resultinfo['typelist'] =$this->Car_type_model->select_list();
			$resultinfo['structurelist'] =$this->Car_structure_model->select_list();
			
			$this->show_view('carInfoEdit',$resultinfo);
			
		}
	}
	//删除车辆信息
	public function delete()
	{
		$carid = $this->input->get_post('carid');
		$result = $this->Car_info_model->delete($carid);
		$this->show_message("删除车辆系列信息成功",site_url("c=car_info&m=index"));
	}
}